package battleship_server;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author omar
 */
public class FilterManager extends Thread {

    private boolean acceptsConnections = true;
    private ServerSocket serverSocket;
    
    private final int port = 7011;

    public FilterManager() {
        try {
            serverSocket = new ServerSocket(port);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    @Override
    public void run(){
        acceptConnections();
    }

    /**
     * Método que acepta conexiones, hasta que acceptConnections sea false.
     */
    public void acceptConnections() {
        while (acceptsConnections && Manager.activeConnections() < 1000) {
            try {
                SocketConnection socketConnection = new SocketConnection(serverSocket.accept());
                socketConnection.start();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(FilterManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (IOException ex) {
                Logger.getLogger(FilterManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
